﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Zero.UControl.Component.Command;

namespace Zero.UControl.Demo.ViewModels
{
    public class ChartWindowModel : INotifyPropertyChanged
    {
        public ChartWindowModel()
        {
            NavigateCommand = new BaseCommand<object>(DoNavigate);
        }


        public event PropertyChangedEventHandler? PropertyChanged;


        private FrameworkElement mainContent;

        public FrameworkElement MainContent
        {
            get { return mainContent; }
            set 
            {
                mainContent = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MainContent"));
            }
        }

        public Dictionary<string,FrameworkElement> ViewCache = new Dictionary<string, FrameworkElement>();

        #region 命令
        public ICommand NavigateCommand { get; set; }

        #endregion


        #region 本地方法
        private void DoNavigate(object obj)
        {
            string? url = obj.ToString();
            if (string.IsNullOrEmpty(url)) return;

            try
            {
                if (!ViewCache.ContainsKey(url))
                {
                    Type? type = Type.GetType(url);
                    if (type == null) return;

                    FrameworkElement ViewObj = (FrameworkElement)Activator.CreateInstance(type);
                    ViewCache.Add(url, ViewObj);
                }

                MainContent = ViewCache[url];
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
        #endregion
    }
}
